<template>
	<view class="goods-item" @click="goToGoodsView">
		<view class="left"><image :src="image" /></view>
		<view class="right">
			<view>
				<view class="text">
					<text class="title">{{ title }}</text>
					<text class="desc">{{ description }}</text>
					<view>
						<text class="price">
							￥
							<text class="price-number">{{ price }}</text>
						</text>
					</view>
				</view>
				<view class="cart-btn">
					<view  class="add_cart" @click.stop.native="addGoodsToCart" v-if="counter === 0"><i class="iconfont icon-gouwuche"></i></view>
					<my-input-number :count="counter" v-if="counter > 0" @changeNumberEvent="getOperator"></my-input-number>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import myInputNumber from '@/components/my-input-number/my-input-number.vue'	
	
import { mapState,mapMutations, mapActions, mapGetters } from 'vuex'
	
export default {
	components: {
		myInputNumber
	},
	name: 'goods-item',
	props: {
		itemId: Number,
		image: String,
		title: String,
		description: String,
		price: String,
		quantity: Number
	},
	data() {
		return {			
			
			
			// 保存到购物车的信息
			goodsItem: {
				id: this.itemId,
				image: this.image,
				title: this.title,
				price: this.price
			}
		};
	},
	computed: {
		
		...mapState([
			'cart'
		]),
		
		
		counter2(){
			
			let that = this;
			
			this.cart.every(item => {
				
				if (item.productId === that.goodsItem.id) {
					return item.quantity;
				}
			});
			return 0;
		},
		
		counter() {
		
		  let that = this;
		
		  let result = 0;
		  this.cart.some(item => {
		
		    // console.log(item.productId +":"+ that.goodsItem.id)
		
		    if (item.productId === that.goodsItem.id) {
		      result += item.quantity;
		      return true;
		    }
		
		  });
		  
		  console.log(result)
		
		  return result;
		},
		
	},
	methods: {
		
		...mapMutations(['ADDGOODS', 'ADDNUM', 'REDUCENUM']),
		
		
		getOperator(op) {			
			
		
			
		  let id = this.goodsItem.id;
		  
		  
		  console.log(op)
		
		  if (op === 'plus') {
		    // this.addNum(id);		
			
			
		    	this.$store.commit('ADDNUM', id);
		  } else {
			  
		    let count = this.cart.filter(val => {
		      return val.productId === id;
		    })[0].quantity;
			
			// console.log("count" + count)
		
		    if (count === 1) {
		      //this.deleteGoodsFromCart(id);
			  this.$store.commit('DELGOODS', id)
		    } else {
		     //  this.jianNum(id);
		     	this.$store.commit('REDUCENUM', id);
		    }
		  }
		},
		
		addGoodsToCart(){
			// this.addGoods(this.goodsItem);
			
			this.$store.commit('ADDGOODS', this.goodsItem);
		},		
		goToGoodsView(){
			uni.navigateTo({
				url: '../home/goodsViews?id=' + this.goodsItem.id
			});
		}
	}
};
</script>

<style lang="scss">
	
	
.add_cart {
	color: #bbb;
	width: 70rpx;
	height: 70rpx;
	border-radius: 40rpx;
	border: 1px solid #aaa;
	line-height: 60rpx;
	padding: 0;
	text-align: center;
	i {
		color: #aaa;
	}
}

.goods-item {
	
	display: flex;
	flex-direction: row; // 默认可以不写
	
	color: #000000;
	
	height: 250rpx;
	width: 100%;
	border-bottom: 1px solid #e4e7ed;
	background-color: #fff;
}



.left {
	width: 40%;
	height: 100%;

	display: flex;

	justify-content: center;
	align-items: center;
}

.left > image {
	width: 100px;
	height: 100px;
	vertical-align: middle;
}

.right {
	display: flex;
	align-items: center;	
	

	box-sizing: border-box;
	position: relative;
	height: 100%;
	width: 60%;
	text-align: left;
	
	.text {
		position: absolute;
		line-height: 24px;
		width: 80%;
		top: 50%;
		transform: translateY(-50%);
	}
	
	.title{
		font-size: 28rpx;
	}	
	.price {
		color: $colorAccent;
		font-weight: bold;
		.price-number {
			font-size: 32rpx;
		}
	}

	.cart-btn {
		position: absolute;
		bottom: 15px;
		right: 15px;
		i {
			font-weight: 600;
		}
	}
}
</style>
